package jindu.zzh.gj.jinduriddle.riddle.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;


import io.reactivex.functions.Consumer;
import jindu.zzh.gj.jinduriddle.R;
import jindu.zzh.gj.jinduriddle.riddle.Myapplication;
import jindu.zzh.gj.jinduriddle.riddle.aiui.IseUtils;


/**
 * Created by ZZH on 2018/11/18.
 */

public class HomeActivity extends BaseAcitivity {
    private Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        requestPermissions();
        handler = new Handler();
    }

    public void startGame(View view) {
//        if (MusicPlayerService.getInstance().isPlaying()) {
//            MusicPlayerService.getInstance().stop();
//        }
//        TTSUtils.getInstance(this).startSpeak("我们禁毒知识问答吧", new SpeakHandler() {
//            @Override
//            public void onSpeakBegin() {
//
//            }
//
//            @Override
//            public void onCompleted(SpeechError var1) {
//                startActivity(new Intent(HomeActivity.this, MainRiddleActivity.class));
//            }
//        });
        startActivity(new Intent(HomeActivity.this, MainRiddleActivity.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (MusicPlayerService.getInstance().isPlaying()) {
//            MusicPlayerService.getInstance().stop();
//        }
        Myapplication.error = 0;
        Myapplication.currentConut = 1;
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                //显示人脸识别
//                if (MainActivity.isMainActivity) return;
//                Log.d("TAG", "run: mainActivity.isMainActivity");
//                if (CameraUtils.checkUsb()) {
//                    //弹出悬浮窗口
//                    ReemanApp.getInstance().setScene(SCENE_NORMAL);
//                    FloatWindowManager.createReadfaceWindow(HomeActivity.this, 1, 1);//readface
//                } else {
//                    SpeechPlugin.getInstance().startSpeak("usb摄像头未挂载");
//                }
//            }
//        }, 1000);

        IseUtils.getInstance(this).cancelEvaluating();
    }


    @Override
    protected void onPause() {
        Log.d("TAG", "home onPause: ");
        //移除人脸识别
        //FloatWindowManager.removeAllView(this);
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d("TAG", "home onNewIntent: ");
    }

    @SuppressLint("CheckResult")
    private void requestPermissions() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission
                .requestEach(
                        Manifest.permission.RECORD_AUDIO)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 用户已经同意该权限
                            Log.d("TAG", permission.name + " is granted.");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                            Log.d("TAG", permission.name + " is denied. More info should be provided.");
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』
                            Log.d("TAG", permission.name + " is denied.");
                        }
                    }
                });
    }
}
